package cn.rxmind.dlicense.core.manager;

import org.springframework.context.ApplicationEventPublisher;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * 客户端证书管理器，定时校验证书合法性
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/14 1:55 下午
 */
public class ScheduleValidateClientLicenseManager extends AbstractClientLicenseManager {

    private ScheduledThreadPoolExecutor scheduledExecutor;

    public ScheduleValidateClientLicenseManager(ApplicationEventPublisher applicationEventPublisher,
                                                Supplier<byte[]> licenseBytesReader) {
        super(applicationEventPublisher,licenseBytesReader);
    }

    @Override
    public void start(){

        this.scheduledExecutor = new ScheduledThreadPoolExecutor(1, r ->{
            Thread thread = new Thread(r);
            thread.setName("validate license thread");
            return thread;
        });

        scheduledExecutor.scheduleWithFixedDelay(this::validateByOrder ,10,60, TimeUnit.SECONDS);
    }
}
